package com.ledim.ledimview;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ledim.activity.MyGoldActivity;
import com.ledim.activity.SwordActivity;
import com.ledim.utils.DensityUtil;
import com.letv.android.young.client.R;

/**
 * Created by qjq on 16/8/17.
 * 战斗力view
 */
public class LedimBaseUserPowerView extends FrameLayout implements View.OnClickListener {

    private Context mContext;
    private TextView mUserGoldSum;
    private ImageView goldImageLevel;
    private TextView mGoldLevelDesStart;
    private TextView mGoldLevelDesEnd;
    private TextView mSwordLevelEnd;
    private TextView mSwordLevelStart;

    private TextView mLeftGoldLevel;
    private TextView mRightGoldLevel;
    private FrameLayout mLeftGoldLayout;

    private LinearLayout mSword;
    private ProgressBar mRocketProgress;
    private int goldLevelAnim;
    private String mGoldCountDes;
    private String mGoldCount;
    private AnimationDrawable animationDrawable = null;
    private ImageView iv_rocket;
    private boolean mIsFromUserInfo;

    public LedimBaseUserPowerView(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public LedimBaseUserPowerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();

    }

    public LedimBaseUserPowerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    /**
     * 初始化view
     */
    public void initView() {
        View userPowerLayout = View.inflate(mContext, R.layout.base_userpower_view, null);

        //用户金币数
        mUserGoldSum = (TextView) userPowerLayout.findViewById(R.id.tv_user_goldsum);

        //奖杯周边的点击事件
        LinearLayout mLevel1 = (LinearLayout) userPowerLayout.findViewById(R.id.ll_level1);
        LinearLayout mLevel2 = (LinearLayout) userPowerLayout.findViewById(R.id.ll_level2);
        goldImageLevel = (ImageView) userPowerLayout.findViewById(R.id.iv_gold_level);
        mLevel1.setOnClickListener(this);
        mLevel2.setOnClickListener(this);
        goldImageLevel.setOnClickListener(this);

        //金币等级上面的文字
        mGoldLevelDesStart = (TextView) userPowerLayout.findViewById(R.id.tv_gold_level_start);
        mGoldLevelDesEnd = (TextView) userPowerLayout.findViewById(R.id.tv_gold_level_end);

        iv_rocket = (ImageView) userPowerLayout.findViewById(R.id.iv_rocket);

        //战斗力等级
        mSwordLevelEnd = (TextView) userPowerLayout.findViewById(R.id.tv_sword_level_end);
        mSwordLevelStart = (TextView) userPowerLayout.findViewById(R.id.tv_sword_level_start);
        //战斗力图标左右两边的显示等级
        mLeftGoldLevel = (TextView) userPowerLayout.findViewById(R.id.tv_left_gold_level);
        mRightGoldLevel = (TextView) userPowerLayout.findViewById(R.id.tv_right_gold_level);
        mLeftGoldLayout = (FrameLayout) userPowerLayout.findViewById(R.id.ll_left_gold_level);

        //自定义progressbar
        mSword = (LinearLayout) userPowerLayout.findViewById(R.id.ll_sword);
        mSword.setOnClickListener(this);
        mRocketProgress = (ProgressBar) userPowerLayout.findViewById(R.id.my_progress);

        addView(userPowerLayout);

    }

    public void initSetting(boolean isFromUserInfo) {
        mIsFromUserInfo = isFromUserInfo;
    }

    /**
     * 选择金币图片
     */
    public void chooseGoldImage(String mUserCoin, String maxCoin) {
        if (!TextUtils.isEmpty(mUserCoin) && !TextUtils.isEmpty(maxCoin)) {
            mUserGoldSum.setText(mUserCoin + "发");
            Double mUserCoins = Double.valueOf(mUserCoin);
            Double AppMaxCoins = Double.valueOf(maxCoin);
            Double current = ((mUserCoins / AppMaxCoins)) * 100;
            if (current == 0) {
                //吃土少年
                goldImageLevel.setImageResource(R.drawable.profile_gold_four);
                goldLevelAnim = R.anim.gold_level_zero_animation;
                mGoldLevelDesStart.setText(R.string.tab_profile_sword_level1);
                mGoldLevelDesEnd.setText(R.string.tab_profile_zero_behind);
            } else if (current > 0 && current <= 60) {
                if (current <= 20) {
                    mGoldLevelDesStart.setText(R.string.tab_profile_sword_level2);
                } else if (current > 20 && current <= 40) {
                    mGoldLevelDesStart.setText(R.string.tab_profile_sword_level3);
                } else {
                    mGoldLevelDesStart.setText(R.string.tab_profile_sword_level4);
                }
                mGoldLevelDesEnd.setText(R.string.tab_profile_tyrant_behind);
                goldImageLevel.setImageResource(R.drawable.profile_gold_three);
                goldLevelAnim = R.anim.gold_level_sixty_animation;
            } else if (current > 60 && current <= 90) {
                if (current <= 80) {
                    mGoldLevelDesStart.setText(R.string.tab_profile_sword_level5);
                } else {
                    mGoldLevelDesStart.setText(R.string.tab_profile_sword_level6);
                }
                goldImageLevel.setImageResource(R.drawable.profile_gold_two);
                goldLevelAnim = R.anim.gold_level_eighty_animation;
                mGoldLevelDesEnd.setText(R.string.tab_profile_tyrant_behind);
            } else if (current > 90 && current <= 100) {
                goldImageLevel.setImageResource(R.drawable.profile_gold_one);
                goldLevelAnim = R.anim.gold_level_hundred_animation;
                mGoldLevelDesStart.setText(R.string.tab_profile_sword_level7);
                mGoldLevelDesEnd.setText(R.string.tab_profile_tyrant_behind);
            }
            mGoldCountDes = mGoldLevelDesStart.getText().toString();
            mGoldCount = mUserCoin;
        }
    }

    /**
     * 设置两边等级及当前进度
     */
    public void getSwordAndShow(String mUserPower) {
        Double userPower = Double.valueOf(mUserPower);
        //设置左边的文字边距
        setLeftLeveMargin(userPower);

        if (userPower >= 0 && userPower <= 100) {
            //LV1
            mSwordLevelStart.setText("LV1");
            mSwordLevelEnd.setText("LV2");
            setCurrentPowerLength(userPower, 100);

        } else if (userPower >= 101 && userPower <= 250) {
            mSwordLevelStart.setText("LV2");
            mSwordLevelEnd.setText("LV3");
            setCurrentPowerLength(userPower, 250);

        } else if (userPower >= 251 && userPower <= 500) {
            mSwordLevelStart.setText("LV3");
            mSwordLevelEnd.setText("LV4");
            setCurrentPowerLength(userPower, 500);

        } else if (userPower >= 501 && userPower <= 800) {
            mSwordLevelStart.setText("LV4");
            mSwordLevelEnd.setText("LV5");
            setCurrentPowerLength(userPower, 800);

        } else if (userPower >= 801 && userPower <= 1200) {
            mSwordLevelStart.setText("LV5");
            mSwordLevelEnd.setText("LV6");
            setCurrentPowerLength(userPower, 1200);

        } else if (userPower >= 1201 && userPower <= 1800) {
            mSwordLevelStart.setText("LV6");
            mSwordLevelEnd.setText("LV7");
            setCurrentPowerLength(userPower, 1800);

        } else if (userPower >= 1801 && userPower <= 2500) {
            mSwordLevelStart.setText("LV7");
            mSwordLevelEnd.setText("LV8");
            setCurrentPowerLength(userPower, 2500);

        } else if (userPower >= 2501 && userPower <= 5000) {
            //LV8
            mSwordLevelStart.setText("LV8");
            mSwordLevelEnd.setText("LV9");
            setCurrentPowerLength(userPower, 5000);
        } else if (userPower >= 5001 && userPower <= 8000) {
            mSwordLevelStart.setText("LV9");
            mSwordLevelEnd.setText("LV10");
            setCurrentPowerLength(userPower, 8000);
        } else if (userPower >= 8001 && userPower <= 12000) {
            mSwordLevelStart.setText("LV10");
            mSwordLevelEnd.setText("LV11");
            setCurrentPowerLength(userPower, 12000);
        } else if (userPower >= 12001 && userPower <= 16000) {
            mSwordLevelStart.setText("LV11");
            mSwordLevelEnd.setText("LV12");
            setCurrentPowerLength(userPower, 16000);
        } else if (userPower >= 16001 && userPower <= 20000) {
            mSwordLevelStart.setText("LV12");
            mSwordLevelEnd.setText("LV13");
            setCurrentPowerLength(userPower, 20000);
        } else if (userPower >= 20001 && userPower <= 30000) {
            mSwordLevelStart.setText("LV13");
            mSwordLevelEnd.setText("LV14");
            setCurrentPowerLength(userPower, 30000);
        } else if (userPower >= 30001 && userPower <= 50000) {
            mSwordLevelStart.setText("LV14");
            mSwordLevelEnd.setText("LV15");
            setCurrentPowerLength(userPower, 50000);
        } else if (userPower >= 50001 && userPower <= 80000) {
            mSwordLevelStart.setText("LV15");
            mSwordLevelEnd.setText("LV16");
            setCurrentPowerLength(userPower, 80000);
        } else if (userPower >= 80001 && userPower <= 120000) {
            mSwordLevelStart.setText("LV16");
            mSwordLevelEnd.setText("LV17");
            setCurrentPowerLength(userPower, 120000);
        } else if (userPower >= 120001 && userPower <= 180000) {
            mSwordLevelStart.setText("LV17");
            mSwordLevelEnd.setText("LV18");
            setCurrentPowerLength(userPower, 180000);
        } else if (userPower >= 180001 && userPower <= 220000) {
            mSwordLevelStart.setText("LV18");
            mSwordLevelEnd.setText("LV19");
            setCurrentPowerLength(userPower, 220000);
        } else if (userPower >= 220001 && userPower <= 26000) {
            mSwordLevelStart.setText("LV19");
            mSwordLevelEnd.setText("LV20");
            setCurrentPowerLength(userPower, 260000);
        } else if (userPower >= 260001 && userPower <= 30000) {
            mSwordLevelStart.setText("LV20");
            mSwordLevelEnd.setText("LV21");
            setCurrentPowerLength(userPower, 260000);
        } else {
            mSwordLevelStart.setText("LV20");
            mSwordLevelEnd.setText("LV21");
            setCurrentPowerLength(userPower, 300001);
        }
    }

    /**
     * 设置当前progressbar进度
     *
     * @param mUserPower
     * @param currentLevelMaxPower
     */
    private void setCurrentPowerLength(double mUserPower, double currentLevelMaxPower) {

        int currentPowerLength = (int) ((mUserPower / currentLevelMaxPower) * 100);
        //判断是否大于20%
        if (currentPowerLength >= 20) {
            mLeftGoldLevel.setVisibility(View.VISIBLE);
            mRightGoldLevel.setVisibility(View.GONE);
            mLeftGoldLevel.setText((int) mUserPower + "/" + (int) currentLevelMaxPower);

        } else {
            mLeftGoldLevel.setVisibility(View.GONE);
            mRightGoldLevel.setVisibility(View.VISIBLE);
            mRightGoldLevel.setText((int) mUserPower + "/" + (int) currentLevelMaxPower);

        }

        DisplayMetrics mDisplayMetrics = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
        int Width = mDisplayMetrics.widthPixels;
        //距离左边的像素
        int progressWidth = (int) ((Width - DensityUtil.dip2px(getContext(), 104f)) *
                (currentPowerLength * 0.01) + DensityUtil.dip2px(getContext(), 52.0f) -
                DensityUtil.dip2px(getContext(), 16.0f));
        mRocketProgress.setProgress(currentPowerLength);

        RelativeLayout.LayoutParams lp =
                (android.widget.RelativeLayout.LayoutParams) iv_rocket.getLayoutParams();
        //设置自己需要的距离
        lp.leftMargin = progressWidth;
    }

    /**
     * 开启金币等级动画
     */
    public void startGoldLevelAnim() {
        if (goldLevelAnim != 0) {
            goldImageLevel.setImageResource(goldLevelAnim);
            animationDrawable = (AnimationDrawable) goldImageLevel.getDrawable();
            animationDrawable.setOneShot(true);
            animationDrawable.start();
        }
    }

    /**
     * 设置左边文字边距
     * @param userPower
     */
    private void setLeftLeveMargin(Double userPower) {
        RelativeLayout.LayoutParams layoutParams =
                (RelativeLayout.LayoutParams) mLeftGoldLayout.getLayoutParams();
        if (userPower >= 80 && userPower < 100) {
            layoutParams.leftMargin = DensityUtil.dip2px(getContext(), -43);
        } else if (userPower >= 100 && userPower < 800) {
            layoutParams.leftMargin = DensityUtil.dip2px(getContext(), -48);
        } else if (userPower >= 800 && userPower < 1000) {
            layoutParams.leftMargin = DensityUtil.dip2px(getContext(), -53);
        } else if (userPower >= 1000 && userPower < 8000) {
            layoutParams.leftMargin = DensityUtil.dip2px(getContext(), -58);
        } else if (userPower >= 8000 && userPower <= 10000) {
            layoutParams.leftMargin = DensityUtil.dip2px(getContext(), -63);
        } else if (userPower >= 10000 && userPower <= 80000) {
            layoutParams.leftMargin = DensityUtil.dip2px(getContext(), -68);
        } else if (userPower >= 80000 && userPower <= 100000) {
            layoutParams.leftMargin = DensityUtil.dip2px(getContext(), -78);
        } else if (userPower >= 100000 && userPower <= 800000) {
            layoutParams.leftMargin = DensityUtil.dip2px(getContext(), -88);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_sword:
                if (!mIsFromUserInfo) {
                    redirect2Sword();
                }
                break;
            default:
                if (!mIsFromUserInfo) {
                    Intent intent = new Intent(mContext, MyGoldActivity.class);
                    intent.putExtra("mGoldCount", mGoldCount);
                    intent.putExtra("mGoldCountDes", mGoldCountDes);
                    mContext.startActivity(intent);
                    ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
                }
                break;
        }
    }

    /**
     * 跳转到金币获取秘籍页面
     */
    public void redirect2Sword() {
        Intent intent = new Intent(mContext, SwordActivity.class);
        mContext.startActivity(intent);
        ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }
}
